package ru.pomarshrutu.client.view;

import java.util.HashMap;

import ru.pomarshrutu.client.controller.Controller;

import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.LargeMapControl;
import com.google.gwt.maps.client.event.MapClickHandler;
import com.google.gwt.maps.client.event.MarkerDragEndHandler;
import com.google.gwt.maps.client.event.MarkerDragStartHandler;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.maps.client.overlay.MarkerOptions;
import com.google.gwt.maps.client.overlay.Overlay;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class UploadPage {

	private final Controller controller;
	
	private VerticalPanel fileUploadPanel;
	private HashMap<String, UploadRecordingWidget> uploadWidgets = new HashMap<String, UploadRecordingWidget>();
//	private UploadRecordingWidget currentUploadWidget;

	private MapWidget mapWidget;
	
	private LatLng draggedMarkerInitialLatLng;

	public UploadPage(Controller controller) {
		this.controller = controller;
	}
	
	public void load() {
		//map widget
		LatLng defaultLocation = LatLng.newInstance(59.938504,30.311279); //St.-Petersburg		

	    mapWidget = new MapWidget(defaultLocation, 10);
	    mapWidget.setSize("500px", "300px");
	    
	    // Add some controls for the zoom level
	    mapWidget.addControl(new LargeMapControl());
	    
	    mapWidget.addMapClickHandler(new MapClickHandler() {
			
			@Override
			public void onClick(MapClickEvent event) {
				MapWidget sender = event.getSender();
		        Overlay overlay = event.getOverlay();
		        LatLng point = event.getLatLng();

		        if(overlay != null && overlay instanceof Marker) { //already have one
		        	LatLng markerPoint = ((Marker)overlay).getLatLng();
		        	UploadRecordingWidget uploadWidget = uploadWidgets.get(markerPoint.toString());
		        	controller.onRemove(uploadWidget);
		        } 
		        else { //new point
		        	MarkerOptions markerOptions = MarkerOptions.newInstance();
		        	markerOptions.setDraggable(true);
		        	Marker marker = new Marker(point, markerOptions);
		        	marker.addMarkerDragStartHandler(new MarkerDragStartHandler() {
						@Override
						public void onDragStart(MarkerDragStartEvent event) {
							draggedMarkerInitialLatLng = event.getSender().getLatLng();							
						}
					});
		        	marker.addMarkerDragEndHandler(new MarkerDragEndHandler() {
						@Override
						public void onDragEnd(MarkerDragEndEvent event) {
							assert(draggedMarkerInitialLatLng != null);
							String initialCoordinates = draggedMarkerInitialLatLng.toString();
							UploadRecordingWidget uploadRecordingWidget = 
								uploadWidgets.get(initialCoordinates);
							uploadWidgets.remove(initialCoordinates);
							String newCoordinates = event.getSender().getLatLng().toString();
							uploadWidgets.put(newCoordinates, uploadRecordingWidget);
							uploadRecordingWidget.setCoordinates(event.getSender().getLatLng());
						}
					});
		        	sender.addOverlay(marker);
		        	UploadRecordingWidget currentUploadWidget = new UploadRecordingWidget(controller);
		        	currentUploadWidget.setCoordinates(point);
		        	currentUploadWidget.setMapMarker(marker);
		        	uploadWidgets.put(point.toString(), currentUploadWidget);
		        	fileUploadPanel.add(currentUploadWidget);
		        }
			}
		});
	    
	    // Add the map to the HTML host page
	    RootPanel.get("mapPanel").add(mapWidget);
		
		fileUploadPanel = new VerticalPanel();
		
		RootPanel.get("fileUploadPanel").add(fileUploadPanel);
	}

	public void removeUploadWidget(UploadRecordingWidget widget) {
		if(widget == null) return;
		
		fileUploadPanel.remove(widget);
		uploadWidgets.remove(widget.getCoordinates().toString());
		mapWidget.removeOverlay(widget.getMapMarker());
	}

	public void displayResponseError(String statusText) {
		Window.alert("Ошибка сервера. " + statusText);
	}
	
}
